package com.g2room.myuall.grap.lx;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.grap.GenericGrap;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.grap.ky.model.po.PeeUnivInfo;
import com.g2room.myuall.grap.lx.model.po.SaUniv;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapLxUniv {
	private static Map<String, String> areasMap = new HashMap<String, String>();
//	private static Map<String, String> areasMap = new HashMap<String, String>();
	static {
		areasMap.put("meiguo","美国");
		areasMap.put("aozhou","澳洲");
		areasMap.put("yingguo","英国");
		areasMap.put("jianada","加拿大");
		areasMap.put("hanguo","韩国");
		areasMap.put("riben","日本");
		areasMap.put("eluosi","俄罗斯");
		areasMap.put("faguo","法国");
		areasMap.put("xinxilan","新西兰");
		areasMap.put("wukelan","乌克兰");
		areasMap.put("ruidian","瑞典");
		areasMap.put("aierlan","爱尔兰");
		areasMap.put("xinjiapo","新加坡");
		areasMap.put("helan","荷兰");
		areasMap.put("malaixiya","马来西亚");
		areasMap.put("nuowei","挪威");
		areasMap.put("deguo","德国");
		areasMap.put("danmai","丹麦");
		areasMap.put("fenlan","芬兰");
		areasMap.put("yidali","意大利");
		areasMap.put("ruishi","瑞士");
		areasMap.put("xibanya","西班牙");
		areasMap.put("aodili","奥地利");
	}

	//http://hanguo.liuxue86.com/school/

	public static void main(String[] args) throws Exception{
		for(String area : areasMap.keySet()) {
			getUnivs(area, areasMap.get(area));
		}
	}
	/**
	 * 获取分页列表
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public static void getUnivs(String code, String country) {
		System.out.println(code + "=" + country);
		Document doc = null;
		String url = "http://"+code+".liuxue86.com/school/";
		try {
			doc = GenericGrap.getDocByIp(url);
			if(doc == null) return;
			Elements divEls = doc.getElementsByAttributeValueContaining("class", "sousuo_new_1 clearfix");
			List<SaUniv> sus = new ArrayList<SaUniv>();
			for(Element divEl : divEls) {
				Elements aEls = divEl.getElementsByTag("a");
				if(null == aEls || aEls.isEmpty()) continue;
				String word = divEl.id();
				for(Element aEl : aEls) {
					SaUniv su = new SaUniv();
					su.setWord(word);
					// <a href="http://hanguo.liuxue86.com/school/7253">安山1大学</a>
//					su.setArea(area);
					su.setCountry(country);
					su.setName(aEl.text());
					su.setUnivUrl(aEl.attr("href"));
					su.setCode(code);
					sus.add(su);
				}
			}
			insert2UnivDb(sus);
		} catch (Exception e) {
			System.out.println(url);
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("31");
			ge.setContent(country);
			ge.setDescription("获取Univ  Page错误："+e.getMessage());
			insert2ErrDb(ge);
		}
	}
	

	public static void insert2UnivDb(List<SaUniv> sus) throws Exception {
		PreparedStatement ps = null;
		try {
			String sql = "insert into sa_univ(code,name,country,area,univ_url,word) values(?,?,?,?,?,?)";
			ps = DbUtil.getConn().prepareStatement(sql);
			for(SaUniv pui : sus) {
				//code,name,country,area,univ_url,word
				ps.setString(1, pui.getCode());
				ps.setString(2, pui.getName());
				ps.setString(3, pui.getCountry());
				ps.setString(4, pui.getArea());
				ps.setString(5, pui.getUnivUrl());
				ps.setString(6, pui.getWord());
				ps.addBatch();
			}
			ps.executeBatch();
			
		} catch(Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
